import pygame
from pygame.locals import *
import field
from colors import random_color
from costants import *
import buttons
import arkanoid
import ranking

pygame.font.init()

try: big_font=pygame.font.Font(FONT,90)
except IOError: big_font=pygame.font.Font(ALTERNATIVE_FONT,90)
#####################################################################
#####################################################################
def play(screen,gioc,level=0,speed=8.0):
	gioc.starting_game(TETRIS)
	pygame.display.update()
	screen=pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT),0,32)
	sfondo=pygame.image.load(BACKGROUND_IMAGE).convert()
	screen.blit(sfondo,(0,0))
	play_board=field.Field(screen)
	play_board.blit_field()
	pygame.display.update()
	ranking.Smiley(FIGHT,"FIGHT!!",screen)
	next=random_color()
	while not play_board.full():
		pezzo=next
		next=random_color()
		screen.blit(sfondo,BALLOON_RECT,BALLOON_RECT)
		screen.blit(sfondo,NAME_TETRIS_RECT,NAME_TETRIS_RECT)
		screen.blit(sfondo,SCORE_RECT,SCORE_RECT)
		screen.blit(next.image,next.image.get_rect(center=BALLOON_CENTER))
		gioc.blit(screen)
		pygame.display.update()
		falling=True
		space_key_free=True
		left_key_free=True
		right_key_free=True
		clock=pygame.time.Clock()
		play_board.blit_field()
		pezzo.blit(screen)
		pygame.display.update(TETRIS_RECT)
		while falling:
			t=0
			dt=clock.tick()
			while t<30:
				play_board.blit_field()
				pezzo.blit(screen)
				pygame.display.update(TETRIS_RECT)
				events=pygame.event.get()
				keys=pygame.key.get_pressed()
				for e in events:
					if e.type==QUIT: return EXIT_GAME
				if keys[K_ESCAPE]:
					choice = pause(screen)
					if choice!=CONTINUE: return choice
					screen.blit(sfondo,(0,0))
					play_board.blit_field()
					gioc.blit(screen)
					screen.blit(next.image,\
					            next.image.get_rect(center=BALLOON_CENTER))
					pygame.display.update()
				if keys[K_LEFT]:
					if left_key_free:
						pezzo.go_left(play_board)
						left_key_free=False
				else:
					left_key_free=1 
				if keys[K_RIGHT]:
					if right_key_free:
						pezzo.go_right(play_board)
						right_key_free=False
				else:
					right_key_free=1
				if keys[K_SPACE]:
					if space_key_free:
						pezzo.rotate(play_board)
						space_key_free=False
				else:
					space_key_free=True
				if keys[K_DOWN]:
					pezzo.go_down(play_board)
				dt=clock.tick()
				play_board.blit_field()
				t+=speed*dt/100
			if pezzo.can_go_down(play_board): pezzo.go_down(play_board)
			else:
				pezzo.fill_mat(play_board)
				play_board.blit_field()
				pygame.display.update(TETRIS_RECT)
				deleted_lines=play_board.remove_complete_lines()
				gioc.increase_deleted_lines(deleted_lines)
				if gioc.pass_level():
					ark_status=arkanoid.play(screen,level,gioc)
					if ark_status==WON: gioc.score.to_double()
					elif ark_status==LOST: gioc.score.to_half()
					else: return ark_status
					level+=1
					speed+=0.5
					tetris_exit=play(screen,gioc,level,speed)
					return tetris_exit
				else: play_board.field_status(deleted_lines)
				pygame.time.wait(200)
				falling=0
			events=pygame.event.get()
			for e in events:
				if e.type==QUIT: return EXIT_GAME
			pygame.time.wait(dt/100)
	return game_over(screen)
########################################################################################
def pause(screen):
	game_pause=True
	menu_bg=pygame.image.load(MENU_BG).convert()
	screen.blit(menu_bg,(0,0))
	pygame.display.update()
	continue_game=buttons.Button((430,410),"CONTINUE GAME",screen)
	back_to_main=buttons.Button((430,460),"BACK TO MAIN",screen)
	quit=buttons.Button((430,510),"EXIT",screen)
	continue_game.blit()
	back_to_main.blit()
	quit.blit()
	while game_pause:
		event=pygame.event.wait()
		if event.type==QUIT: return EXIT_GAME
		if event.type==MOUSEBUTTONDOWN:
			if continue_game.is_pressed(event.dict): return CONTINUE
			if back_to_main.is_pressed(event.dict): return TO_MAIN
			if quit.is_pressed(event.dict): return EXIT_GAME
#######################################################################			
def game_over(screen):
	while 1:
		lost=big_font.render("GAME OVER!",True,BLACK)
		screen.blit(lost,lost.get_rect(center=(440,260)))
		ranking.Smiley(LOST,"YOU LOST!",screen)
		event=pygame.event.wait()
		if event.type==QUIT: return EXIT_GAME
		if event.type==KEYDOWN or event.type==MOUSEBUTTONDOWN: return TO_MAIN
##########################################################################################
################################################################################
